package com.yuejian_06.yuejian_06.fragment;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.adapter.OrderAdapter;
import com.yuejian_06.yuejian_06.adapter.OrderCourseAdapter;
import com.yuejian_06.yuejian_06.bean.OrderCourseBean;
import com.yuejian_06.yuejian_06.bean.OrderListBean;
import com.yuejian_06.yuejian_06.model.MeModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class OCFragment extends Fragment implements View.OnClickListener  {


    private RecyclerView recyclerView;
    private SwipeRefreshLayout activity_order_list;

    public OCFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_oc, container, false);
        recyclerView = (RecyclerView) v.findViewById(R.id.ocl_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        SharedPreferences pref = getActivity().getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = pref.getString("user_id","0");//第二个参数为默认值
        MeModel meModel = new MeModel();
        meModel.getOrderCourseList(user_id,meListener);

        //下拉刷新
        activity_order_list = (SwipeRefreshLayout) v.findViewById(R.id.foc);
        activity_order_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override public void onRefresh() {
                //重新获取数据
                SharedPreferences pref = getActivity().getSharedPreferences("userInfo",MODE_PRIVATE);
                String user_id = pref.getString("user_id","0");//第二个参数为默认值
                MeModel meModel = new MeModel();
                meModel.getOrderCourseList(user_id,meListener1);
                //获取完成
                activity_order_list.setRefreshing(false);
            }
        });
        return v;
    }

    private MeListener<List<OrderCourseBean>> meListener = new MeListener<List<OrderCourseBean>>() {
        @Override
        public void onResponse(List<OrderCourseBean> o) {
            List<OrderCourseBean> res=o;
            List<OrderCourseBean> mDataList =(List) res;

            recyclerView.setAdapter(new OrderCourseAdapter(getActivity(),mDataList));
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(),"F失败了",Toast.LENGTH_SHORT).show();
        }
    };

    //刷新
    private MeListener<List<OrderCourseBean>> meListener1 = new MeListener<List<OrderCourseBean>>() {
        @Override
        public void onResponse(List<OrderCourseBean> o) {
            List<OrderCourseBean> res=o;
            List<OrderCourseBean> mDataList =(List) res;
            recyclerView.setAdapter(new OrderCourseAdapter(getActivity(),mDataList));
            Toast.makeText(getActivity(),"课程订单刷新成功",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(),"F刷新失败了",Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    public void onClick(View v) {

    }
}
